<template>
  <div>
    <el-dialog
      title="新增资源"
      :visible.sync="dialogVisible"
      width="700px"
      @closed="resetForm('form')"
      :close-on-click-modal="false"
    >
      <div>
        <el-form
          ref="form"
          :rules="rules"
          :model="form"
          label-width="120px"
          style="min-height: 300px"
        >
          <el-form-item label="选择资源" prop="name">
            <UploadFile v-if="dialogVisible" multiple @e-fileData="saveFile" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button  type="custom" v-preventReClick   @click="submitForm('form')">提 交</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  sysKnowledgeFilesAdd,
  sysKnowledgeFilesUpdate,
} from "@/api/admin/courseLibrary/sysKnowledgeFiles.js";
import UploadFile from "@/components/uploadFile/index.vue";
import Bus from "@/utils/bus.js";
export default {
  components: {
    UploadFile,
  },
  data() {
    return {
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        fileId: "", //资源ID
        knowledgeId: this.$store.getters.knowledgeId, //知识点ID
        // des: "", //资源介绍
      },
      rules: {
        fileId: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
    };
  },
  mounted() {
    Bus.$on("showDialogKnowledgeFilesAdd", (v) => {
      if (v) {
        this.checkedData = v;
        this.handleAddOrEdit(2);
      } else {
        this.handleAddOrEdit(1);
      }
    });
  },
  beforeDestroy() {
    Bus.$off("showDialogKnowledgeFilesAdd");
  },
  methods: {
    saveFile(v) {
      this.form.fileId = v.map((item) => item.id).toString();
    },
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = "编辑";
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || "";
          }
        }
        this.form.id = this.checkedData.id;
      } else {
        this.title = "新增";
        delete this.form.id;
      }
      this.addOrEdit = type;
      this.dialogVisible = true;
    },
    submitForm(form) {
      if (!this.form.fileId) {
        this.$message({
          message: "请上传文件",
          type: "warning",
        });
        return;
      }
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit();
        }
      });
    },
    //新增
    add() {
      sysKnowledgeFilesAdd(this.form).then((res) => {
        this.dialogVisible = false;
        Bus.$emit("refreshKnowledgeFilesList");
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
    //编辑
    edit() {
      sysKnowledgeFilesUpdate(this.form).then((res) => {
        this.dialogVisible = false;
        Bus.$emit("refreshKnowledgeFilesList");
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields();
      this.form.fileId = "";
    },
  },
};
</script>